package com.wisdytech.ccdserver.common.config;

import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.message.BasicHeader;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;

/**
 * @author sunjinxin
 * @date 2024/5/21
 */
public class RestHighLevelClientConfig {
    String node1_ipaddr = "10.36.23.87";
    String node2_ipaddr = "10.36.23.81";

    int node1_rest_api_port = 9200;
    int node2_rest_api_port = 9200;

    String node1_rest_api_protocol = "http";
    String node2_rest_api_protocol = "http";


    @Bean
    public RestHighLevelClient simpleInitClient() {
        RestClientBuilder builder = RestClient.builder(
                new HttpHost(node1_ipaddr, node1_rest_api_port, node1_rest_api_protocol),            //es集群节点1 httpHost信息
                new HttpHost(node2_ipaddr, node2_rest_api_port, node2_rest_api_protocol));          //es集群节点2 httpHost信息
        //设置http请求头，例如以下示例展示添加es basic安全认证鉴权信息
        Header[] defaultHeaders = new Header[]{new BasicHeader("Authorization", "Basic bW9zOjEyMzQ1")};
        builder.setDefaultHeaders(defaultHeaders);                //添加头信息
        return new RestHighLevelClient(builder);
    }
}